<template>
  <div>
    <div class="shopcart" >
      <div class="content">
        <div class="content-left" @click="toggleShow">
          <div class="logo-wrapper">
            <div class="logo" :class="{highlight: totalCount}">
              <i class="iconfont icon-gouwuche1" :class="{highlight: totalCount}"></i>
            </div>
            <div class="num" v-if="totalCount">{{totalCount}}</div>
          </div>
          <div class="price" :class="{highlight: totalCount}">￥{{totalPrice}}</div>
          <div class="desc">另需配送费￥{{info.deliveryPrice}}元</div>
        </div>
        <div class="content-right">
          <div class="pay" :class="payClass">
            {{payText}}
          </div>
        </div>
      </div>
      <transition name="move">
        <div class="shopcart-list" v-show="listShow">
          <div class="list-header">
            <h1 class="title">购物车</h1>
            <span class="empty" @click="clearCart">清空</span>
          </div>
          <div class="list-content">
            <ul >
              <li class="food" v-for="(food, index) of cartFoods" :key="index">
                <span class="name">{{food.name}}</span>
                <div class="price"><span>￥{{food.price}}</span></div>
                <div class="cartcontrol-wrapper">
                  <cart-control :food="food"/>
                </div>
              </li>
            </ul>
          </div>
         </div>
      </transition>
    </div>
    <div class="list-mask" v-show="listShow" @click="toggleShow"></div>
  </div>
</template>

<script>
import CartControl from '../cartcontrol/CartControl'
import {MessageBox} from 'mint-ui'
import BScroll from 'better-scroll'
import {mapState, mapGetters} from 'vuex'
export default {
  name: 'ShopCart',
  components: {
    CartControl,
    BScroll
  },
  data () {
    return {
      isShow: false
    }
  },
  computed: {
    ...mapState(['cartFoods', 'info']), // 上边调用
    ...mapGetters(['totalCount', 'totalPrice']),
    payClass () { // 当前总价格totalPrice 当前数量totalCount
      const {totalPrice} = this // const totalPrice = this.totalPrice; this应该是一个对象，this中肯定也有一个tot变量，就是将this.totalPrice赋值给totalPrice
      const {minPrice} = this.info // 起送价格价格
      return totalPrice >= minPrice ? 'enough' : 'not-enough'
    },
    payText () {
      const {totalPrice} = this // const totalPrice = this.totalPrice; this.totalPrice是一个对象，this中肯定也有一个tot变量，就是将this.totalPrice赋值给totalPrice
      const {minPrice} = this.info
      if (totalPrice === 0) {
        return `￥${minPrice}元起送`
      } else if (totalPrice < minPrice) {
        return `还差￥${minPrice - totalPrice}元起送`
      } else {
        return '去结算'
      }
    },
    listShow () {
      // 如果总数量为0 ，直接不显示
      if (this.totalCount === 0) {
        this.isShow = false
        return false // 结果是false
      }
      if (this.isShow) { // 判断购物车列表显示时则启用better-scroll
        this.$nextTick(() => { // 启动回调函数
          if (!this.scroll) { // 判断scroll存在不，实现BScroll的实例是单例
            this.scroll = new BScroll('.list-content', { // 当scroll存在则用this.scroll保存起来
              click: true
            })
          } else {
            this.scroll.refresh() // refresh：刷新的意思。让滚动条重新刷新下，重新统计内容的高度
          }
        })
      }
      return this.isShow // 把上一级的结果返回到这
    }
  },
  methods: {
    toggleShow () {
      // 只有当总数量大于0时候才切换
      if (this.totalCount > 0) {
        this.isShow = !this.isShow
      }
    },
    clearCart () {
      MessageBox.confirm('确定清空购物车吗？').then(action => {
        this.$store.dispatch('clearCart')
      }, () => {}) // () => {} 点击取消的操作 如果不写会报错
    }
  }
}
</script>

<style lang="stylus">
  @import "../../common/stylus/mixins.styl"
  .mint-msgbox-message
    line-height 150px
    font-size 30px
  .mint-msgbox-title
    margin-top 30px
    font-size 30px
  .mint-msgbox-btns
    height 80px
    font-size 30px
  .shopcart
    position fixed
    left 0
    bottom 0
    z-index 50
    width 100%
    height 96px
    .content
      display flex
      background #141d27
      font-size 0
      color rgba(255, 255, 255, 0.4)
      .content-left
        flex 1
        .logo-wrapper
          display inline-block
          vertical-align top
          position relative
          top -20px
          margin 0 24px
          padding 12px
          width 112px
          height 112px
          box-sizing border-box
          border-radius 50%
          background #141d27
          .logo
            width 100%
            height 100%
            border-radius 50%
            text-align center
            background #2b343c
            &.highlight
              background $green
            .icon-gouwuche1
              line-height 88px
              font-size 48px
              color #80858a
              &.highlight
                color #fff
          .num
            position absolute
            top 0
            right 0
            width 48px
            height 32px
            line-height 32px
            text-align center
            border-radius 32px
            font-size 18px
            font-weight 700
            color #ffffff
            background rgb(240, 20, 20)
            box-shadow 0 8px 16px 0 rgba(0, 0, 0, 0.4)
        .price
          display inline-block
          vertical-align top
          margin-top 10px
          line-height 48px
          padding-right 24px
          box-sizing border-box
          font-size 32px
          font-weight 700
          color #fff
          &.highlight
            color #fff
        .desc
          display inline-block
          vertical-align bottom
          margin-bottom 30px
          margin-left -90px
          font-size 20px
      .content-right
        flex 0 0 210px
        width 210px
        .pay
          height 96px
          line-height 96px
          text-align center
          font-size 26px
          font-weight 700
          letter-spacing 8px
          text-indent 4px
          color #fff
          &.not-enough
            background #2b333b
          &.enough
            background #00b43c
            color #fff
    .ball-container
      .ball
        position fixed
        left 64px
        bottom 44px
        z-index 200
        transition all 0.4s cubic-bezier(0.49, -0.29, 0.75, 0.41)
        .inner
          width 32px
          height 32px
          border-radius 50%
          background $green
          transition all 0.4s linear
    .shopcart-list
      position absolute
      left 0
      top 0
      z-index -1
      width 100%
      transform translateY(-100%) // 隐藏在页面底下
      &.move-enter-active, &.move-leave-active
        transition transform .3s
      &.move-enter, &.move-leave-to
        transform translateY(0) // 购物车列表显示
      .list-header
        height 80px
        line-height 80px
        padding 0 36px
        background #f3f5f7
        border-bottom 2px solid rgba(7, 17, 27, 0.1)
        .title
          float left
          font-size 28px
          color rgb(7, 17, 27)
        .empty
          float right
          font-size 24px
          color rgb(0, 160, 220)
      .list-content
        padding 0 36px
        max-height 300px
        overflow hidden
        background #fff
        .food
          position relative
          padding 24px 0
          box-sizing border-box
          bottom-border-1px(rgba(7, 17, 27, 0.1))
          .name
            line-height 48px
            font-size 28px
            color rgb(7, 17, 27)
          .price
            position absolute
            right 180px
            bottom 24px
            line-height 48px
            font-size 28px
            font-weight 700
            color rgb(240, 20, 20)
          .cartcontrol-wrapper
            position absolute
            right 0
            bottom 25px

  .list-mask
    position fixed
    top 0
    left 0
    width 100%
    height 100%
    z-index 40
    backdrop-filter blur(20px)
    opacity 1
    background rgba(7, 17, 27, 0.6)
    &.fade-enter-active, &.fade-leave-active
      transition all 0.5s
    &.fade-enter, &.fade-leave-to
      opacity 0
      background rgba(7, 17, 27, 0)
</style>
